package com.edisoncgh.service.impl;

import com.edisoncgh.dao.BookMapper;
import com.edisoncgh.pojo.Book;
import com.edisoncgh.pojo.BookExample;
import com.edisoncgh.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * BookServiceImpl注入到IOC容器中
 * 通过\@Service和\@Autowired注解实现
 * 可参考：https://blog.csdn.net/yangguosb/article/details/84594129
 */
@Service
public class BookServiceImpl implements BookService {
    // 设置一个set接口来调用dao层的操作
    private BookMapper bookMapper;
    // \@Autowired注解如果加在变量上会提示"Field injection is not recommended"
    // 所以我选择加载构造方法上
    // 可参考：https://blog.csdn.net/zhangjingao/article/details/81094529

    @Autowired
    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    @Override
    public boolean addBook(Book book) {
        return bookMapper.insert(book)>0;
    }

    @Override
    public boolean deleteBookById(String ISBN) {
        return bookMapper.deleteByPrimaryKey(ISBN)>0;
    }

    @Override
    public boolean updateBook(Book book) {
        return bookMapper.updateByPrimaryKey(book)>0;
    }

    @Override
    public Book findBookById(String ISBN) {
        return bookMapper.selectByPrimaryKey(ISBN);
    }

    @Override
    public List<Book> findAllBook() {
        BookExample bookExample = new BookExample();
        return bookMapper.selectByExample(bookExample);
    }


}
